import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// 解决导航栏或者底部导航tabBar中的vue-router在3.0版本以上频繁点击菜单报错的问题。
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  {
    path: '/',
    name: 'Manager',
    component: () => import('../views/manager/Manager.vue'),
    children: [
      { path: '403', name: 'NoAuth', meta: { name: '无权限' }, component: () => import('../views/common/403.vue') },
      { path: 'home', name: 'Home', meta: { name: '系统首页' }, component: () => import('../views/home/Home.vue') },
      { path: 'admin', name: 'Admin', meta: { name: '管理员信息' }, component: () => import('../views/admin/Admin.vue') },
      { path: 'adminPerson', name: 'AdminPerson', meta: { name: '个人信息' }, component: () => import('../views/manager/AdminPerson') },
      { path: 'password', name: 'Password', meta: { name: '修改密码' }, component: () => import('../views/manager/Password') },
      { path: 'categories', name: 'Categories', meta: { name: '分类信息' }, component: () => import('../views/categories/Categories.vue') },
      { path: 'order', name: 'Order', meta: { name: '订单信息' }, component: () => import('../views/order/Order.vue') },
      { path: 'product', name: 'Product', meta: { name: '商品信息' }, component: () => import('../views/product/Product.vue') },
      { path: 'specifications', name: 'ProductSpecifications', meta: { name: '商品规格信息' }, component: () => import('../views/specifications/Specifications.vue') },
      { path: 'sales', name: 'Sales', meta: { name: '出售商品信息' }, component: () => import('../views/sales/Sales.vue') },
      { path: 'employee', name: 'Employee', meta: { name: '员工信息' }, component: () => import('../views/employee/Employee.vue') },
      { path: 'activity', name: 'Activity', meta: { name: '广告信息' }, component: () => import('../views/activity/Activity.vue') },
      { path: 'member', name: 'Member', meta: { name: '会员信息' }, component: () => import('../views/member/Member.vue') },
      { path: 'credit', name: 'Credit', meta: { name: '信用分信息' }, component: () => import('../views/credit/Credit.vue') },
      { path: 'comments', name: 'Comments', meta: { name: '评论审核' }, component: () => import('../views/comments/Comments.vue') },
      { path: 'salesAfter', name: 'SalesAfter', meta: { name: '售后信息' }, component: () => import('../views/salesAfter/SalesAfter.vue') },
    ]
  },
  {
    path: '/front',
    name: 'Front',
    component: () => import('../views/Front.vue'),
    children: [
      { path: 'home', name: 'Home', meta: { name: '系统首页' }, component: () => import('../views/front/Home') },
      { path: 'person', name: 'Person', meta: { name: '个人信息' }, component: () => import('../views/front/Person') },
    ]
  },
  { path: '/login', name: 'Login', meta: { name: '登录' }, component: () => import('../views/login/Login.vue') },
  { path: '/register', name: 'Register', meta: { name: '注册' }, component: () => import('../views/login/Register.vue') },
  { path: '*', name: 'NotFound', meta: { name: '无法访问' }, component: () => import('../views/common/404.vue') },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  let user = JSON.parse(localStorage.getItem("xm-user") || '{}');
  // 检查是否是根路径
  if (to.path === '/login') {
    if (user.token) {
      next('/home') // 如果存在token，则重定向到主页
    } else {
      next() // 如果不存在token，正常进行路由
    }
  } else {
    // 对于非根路径，判断是否有token
    if (user.token) {
      next() // 如果存在token，正常进行路由
    } else {
      next('/login') // 如果不存在token，则重定向到登录页
    }
  }
})

export default router
